package com.heima.behavior.service.impl;

import com.alibaba.fastjson.JSON;
import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.behavior.service.ApReadBehaviorService;
import com.heima.common.constants.article.HotArticleConstants;
import com.heima.common.exception.CustException;
import com.heima.model.behavior.dtos.ReadBehaviorDTO;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApReadBehavior;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.mess.UpdateArticleMess;
import com.heima.model.threadlocal.AppThreadLocalUtils;
import com.heima.model.user.pojos.ApUser;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ApReadBehaviorServiceImpl implements ApReadBehaviorService {


    @Autowired
    ApBehaviorEntryService apBehaviorEntryService;

    @Autowired
    MongoTemplate mongoTemplate;

    @Autowired
    RabbitTemplate rabbitTemplate;

    @Override
    public ResponseResult readBehavior(ReadBehaviorDTO dto) {

     //   1. 校验参数 文章id必须传
        if(dto==null){
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID);
        }
     //   2. 根据登录用户id 或 设备id查询行为实体数据(阅读操作可不登录)

        ApUser user = AppThreadLocalUtils.getUser();
        if(user==null){
            CustException.cust(AppHttpCodeEnum.NEED_LOGIN);
        }
        ApBehaviorEntry behaviorEntry = apBehaviorEntryService.findByUserEquipmentId(user.getId(), dto.getEquipmentId());

        Query query = Query.query(Criteria.where("entryId").is(behaviorEntry.getId()));
        ApReadBehavior one = mongoTemplate.findOne(query, ApReadBehavior.class);
        //   3. 判断阅读行为是否存在
        if(one==null){
            //   5. 不存在  创建阅读行为 并初始化count字段值为 1
            ApReadBehavior readBehavior = new ApReadBehavior();
            readBehavior.setCreatedTime(new Date());
            readBehavior.setEntryId(behaviorEntry.getId());
            readBehavior.setCount((short) 1);
            readBehavior.setArticleId(dto.getArticleId());
            readBehavior.setReadDuration(dto.getReadDuration());
            readBehavior.setPercentage(dto.getPercentage());
            readBehavior.setLoadDuration(dto.getLoadDuration());
            mongoTemplate.save(readBehavior);

        }else {
            //   4. 存在  将阅读行为的count字段加1 并修改
            int i = one.getCount() + 1;
            Query query1 = Query.query(Criteria.where("articleId").is(dto.getArticleId()));
            Update update = new Update();
            update.set("count",i).set("createdTime",new Date());
            mongoTemplate.updateMulti(query1,update,ApReadBehavior.class);
        }
        UpdateArticleMess mess = new UpdateArticleMess();
        mess.setType(UpdateArticleMess.UpdateArticleType.VIEWS);
        mess.setArticleId(dto.getArticleId());
        mess.setAdd(1);
        //阅读行为发送消息
        rabbitTemplate.convertAndSend(HotArticleConstants.HOT_ARTICLE_SCORE_BEHAVIOR_QUEUE, JSON.toJSONString(mess));
        return ResponseResult.okResult();
    }
}
